package com.kucowka.struts.actions;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.interceptor.ServletRequestAware;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.validator.annotations.RequiredStringValidator;
import com.opensymphony.xwork2.validator.annotations.Validation;

@Validation
public class WelcomeUserAction extends ActionSupport implements
		ServletRequestAware {

	Logger logger = LoggerFactory.getLogger(WelcomeUserAction.class);

	private int counter = 1;

	private String username;
	private String password;
	private String role;
	private String email;
	private String ipAddress;

	private HttpServletRequest httpServletRequest;

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getRole() {
		return role;
	}

	public void setRole(String role) {
		this.role = role;
	}

	@RequiredStringValidator(message = "Password is required 2")
	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	// all struts logic here
	public String getIpAddress() {
		return ipAddress;
	}

	@Override
	public void setServletRequest(HttpServletRequest httpServletRequest) {
		this.httpServletRequest = httpServletRequest;
	}

	public String execute() {

		if (!getUsername().equals("Admin")) {

			addActionError(getText("login.error"));

			return ERROR;
		}

		ipAddress = httpServletRequest.getRemoteHost();

		logger.info("Welcome action was executed " + counter++ + " times");

		logger.info("Selected roles is: " + role);

		return SUCCESS;

	}

}
